package com.cskaoyan._14access._04usecase.factory;

import java.lang.reflect.Constructor;

/**
 * @description:
 * @author: 景天
 * @date: 2023/2/7 16:06
 **/

public class StudentFactory {
    private StudentFactory() {
    }

    // 提供一个静态的方法 给外部访问
    public static Student getInstance(String className, String name, int age) throws Exception {

        //Student student = new Student(name, age);
        // 要使用反射
        Class<?> c = Class.forName(className);

        Constructor<?> constructor = c.getDeclaredConstructor(String.class, int.class);
        constructor.setAccessible(true);
        // 创建学生对象
        Student student = (Student) constructor.newInstance(name, age);
        // 最终 返回一个学生对象
        return student;
    }
}
